package com.itqf.controller;

import com.itqf.pojo.Student;
import com.itqf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * projectName: springmvcgp8
 *
 * @author: 赵伟风
 * time: 2021/8/5 17:48
 * description:
 */

@RequestMapping("param")
@Controller
public class ParamterController {


    /**
     * 想要获取请求和响应对象，直接在形参列表添加即可！
     * @param request
     * @param response
     * @return
     */
    @GetMapping("web")
    @ResponseBody
    public String web(HttpServletRequest request, HttpServletResponse response){

        StringBuffer requestURL = request.getRequestURL();

        return requestURL.toString();

    }


    /**
     *
     * JSON - Javascript Object Notation js的一种对象表达形式
     *
     *     var student = new Object();
     *     student.name = "";
     *     student.age  = "";
     *
     *     var studentJson = {name:"xx",age:11}
     *
     * 后台： 对象表达形式取值
     *
     *     Student
     *        name
     *        age
     *
     */

    @PostMapping(value = "save",consumes = "application/json;charset=utf-8")
    @ResponseBody
    public Student save(@RequestBody Student student){

        System.out.println("ParamterController.save");

        return  student;
    }



    /**
     *
     *  handlerMapping  - map key = handler标识 【请求方式 url】
     *
     * 形参列表： 参数 = 请求的参数名  ？id=1
     * @param id
     * @return
     */
    @DeleteMapping("/{idx}")
    public String remove(@PathVariable(name="idx") int id){
        System.out.println("id = " + id);

        return "您删除了商品：id="+id;
    }


    @GetMapping("/{idx}")
    public String get(@PathVariable(name="idx") int id){
        System.out.println("id = " + id);

        return "您查询了商品：id="+id;
    }



    /**
     * 要求参数名和类型必须和实体类一致
     *    实体类必须重写get/set方法
     * @param user
     * @return
     */
    @GetMapping("register")
    @ResponseBody
    public User register(User user){
        System.out.println("user = " + user);
        return user;
    }


    /**
     *  1.形参列表简化大法
     *     形参的命名、类型和请求参数一致，就会自动赋值！ 【handlerAdapter】
     *  /param/login  post
     *     username root
     *     password root
     * @return
     */
    @PostMapping("login")
    @ResponseBody
    public  String  getParam(String username,String password){

        return username + ":::" +password;
    }



    /**
     *  2.形参列表简化大法
     *     形参的命名和请求参数不致！
     *     参数前 @RequestParam(name="请求参数名")
     *  /param/login  post
     *     username root
     *     password root
     * @return
     */
    @PostMapping("login2")
    @ResponseBody
    public  String  getParam2(String username,@RequestParam(name = "password") String pwd){

        return username + ":::" +pwd;
    }


    /**
     *  3.形参列表简化大法
     *     参数默认值
     *     @RequestParam(defaultValue = "1")
     *  /param/login  post
     *     username root
     *     password root
     * @return
     */
    @PostMapping("login3")
    @ResponseBody
    public  String  getParam3(String username,@RequestParam(name = "password") String pwd,@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "4") int pageSize){
        System.out.println("username = " + username + ", pwd = " + pwd + ", page = " + page + ", pageSize = " + pageSize);
        return username + ":::" +pwd;
    }




}
